package com.bilibili.juc.volatileList;

import java.util.concurrent.TimeUnit;

/**
 * @author zuife
 * @description
 * @since 2024-05-01 08:30:38
 * 非原子性案例
 */
class MyNumber {
    int number = 0;

    public synchronized void addPlusPlus() {
        number++;
    }
}

public class VolatileNoAtomicDemo {
    volatile static boolean flag = true;

    public static void main(String[] args) {
        MyNumber myNumber = new MyNumber();
        for (int i = 1; i <= 10; i++) {
            new Thread(() -> {
                for (int j = 1; j <= 1000; j++){
                    myNumber.addPlusPlus();
                }
            },String.valueOf(1)).start();
        }
    //暂停几秒钟
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(myNumber.number);
    }
}
